package com.example.yls.demoa;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import java.io.File;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddActivity extends AppCompatActivity implements View.OnClickListener {
    private String val;
    private Button savebtn, deletebtn;
    private EditText ettext;
    private ImageView c_img;
    private VideoView v_video;
    private Notesdb notesdb;
    private SQLiteDatabase dbwriter;
    private File phonefile, videofile;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        //主界面点击button传递过来的数据
        val = getIntent().getStringExtra("flag");

        savebtn = (Button) findViewById(R.id.save);
        deletebtn = (Button) findViewById(R.id.delete);
        ettext = (EditText) findViewById(R.id.ettext);
        c_img = (ImageView) findViewById(R.id.c_img);
        v_video = (VideoView) findViewById(R.id.c_video);

        savebtn.setOnClickListener((View.OnClickListener) this);
        deletebtn.setOnClickListener((View.OnClickListener) this);

        notesdb = new Notesdb(this);
        dbwriter = notesdb.getWritableDatabase();

        initview();
    }
    //判断存储的是文字，图片，还是视频，启动相对应的控件
    public void initview() {
        if (val.equals("1")) { // 文字
            c_img.setVisibility(View.GONE);
            v_video.setVisibility(View.GONE);
        }
        if (val.equals("2")) {
            c_img.setVisibility(View.VISIBLE);
            v_video.setVisibility(View.GONE);

            Intent img = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            phonefile = new File(Environment.getExternalStorageDirectory()
                    .getAbsoluteFile() + "/" + gettime() + ".jpg");
            img.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(phonefile));
            startActivityForResult(img, 1);
        }
        if (val.equals("3")) {
            c_img.setVisibility(View.GONE);
            v_video.setVisibility(View.VISIBLE);

            Intent video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            videofile = new File(Environment.getExternalStorageDirectory()
                    .getAbsoluteFile() + "/" + gettime() + ".mp4");
            video.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videofile));
            startActivityForResult(video, 2);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                adddb();
                finish();
                break;

            case R.id.delete:
                finish();
                break;
        }
    }
    //存储数据
    public void adddb() {
        ContentValues cv = new ContentValues();
        cv.put(notesdb.content, ettext.getText().toString());
        cv.put(notesdb.time, gettime());
        cv.put(notesdb.path, phonefile + "");
        cv.put(notesdb.video, videofile + "");
        dbwriter.insert(notesdb.table_name, null, cv);
    }
    //时间的显示
    private String gettime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
        Date curdate = new Date();
        String str = format.format(curdate);
        return str;
    }
    //完成数据拍摄后直接显示数据
    @Override
    protected void onActivityResult(int requestcode, int resultcode, Intent Data) {
        super.onActivityResult(requestcode, resultcode, Data);

        if (requestcode == 1) {
            Bitmap bitmap = BitmapFactory.decodeFile(phonefile
                    .getAbsolutePath());
            c_img.setImageBitmap(bitmap);
        }
        if (requestcode == 2) {
            v_video.setVideoURI(Uri.fromFile(videofile));
            v_video.start();
        }
    }
}
